package com.luo.chengrui.module.crbpm.service.orgdefinition;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.luo.chengrui.module.crbpm.controller.admin.orgdefinition.vo.*;
import com.luo.chengrui.module.crbpm.dal.dataobject.orgdefinition.BpmOrgDefinitionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;

import com.luo.chengrui.module.crbpm.convert.orgdefinition.BpmOrgDefinitionConvert;
import com.luo.chengrui.module.crbpm.dal.mysql.orgdefinition.BpmOrgDefinitionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.crbpm.enums.ErrorCodeConstants.*;

/**
 * 组织机构流程，定义各组织内部可发起的流程类型。  Service 实现类
 *
 * @author luodz
 */
@Service
@Validated
public class BpmOrgDefinitionServiceImpl implements BpmOrgDefinitionService {

    @Resource
    private BpmOrgDefinitionMapper bpmOrgDefinitionMapper;

    @Override
    public String createBpmOrgDefinition(BpmOrgDefinitionCreateReqVO createReqVO) {
        // 插入
        BpmOrgDefinitionDO bpmOrgDefinition = BpmOrgDefinitionConvert.INSTANCE.convert(createReqVO);
        bpmOrgDefinition.setId(createReqVO.getOrgId());
        if (bpmOrgDefinitionMapper.selectById(bpmOrgDefinition.getId()) != null) {
            throw exception(BPM_ORG_DEFINITION_EXISTS);
        }
        bpmOrgDefinitionMapper.insert(bpmOrgDefinition);
        // 返回
        return bpmOrgDefinition.getId();
    }

    @Override
    public void updateBpmOrgDefinition(BpmOrgDefinitionUpdateReqVO updateReqVO) {
        // 校验存在
        validateBpmOrgDefinitionExists(updateReqVO.getId());
        // 更新
        BpmOrgDefinitionDO updateObj = BpmOrgDefinitionConvert.INSTANCE.convert(updateReqVO);
        bpmOrgDefinitionMapper.updateById(updateObj);
    }

    @Override
    public void deleteBpmOrgDefinition(String id) {
        // 校验存在
        validateBpmOrgDefinitionExists(id);
        // 删除
        bpmOrgDefinitionMapper.deleteById(id);
    }

    private void validateBpmOrgDefinitionExists(String id) {
        if (bpmOrgDefinitionMapper.selectById(id) == null) {
            throw exception(BPM_ORG_DEFINITION_NOT_EXISTS);
        }
    }

    private void validateBpmOrgDefinitionExists(String id, SFunction<BpmOrgDefinitionDO, ?> field, Object value, final ErrorCode errorCode) {
        BpmOrgDefinitionDO entity = bpmOrgDefinitionMapper.selectOne(field, value);
        if (entity == null) {
            return;
        }
        if (id == null || !Objects.equals(entity.getId(), id)) {
            throw exception(errorCode);
        }
    }

    @Override
    public BpmOrgDefinitionDO getBpmOrgDefinition(String id) {
        return bpmOrgDefinitionMapper.selectById(id);
    }

    @Override
    public List<BpmOrgDefinitionDO> getBpmOrgDefinitionList(Collection<String> ids) {
        return bpmOrgDefinitionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BpmOrgDefinitionDO> getBpmOrgDefinitionPage(BpmOrgDefinitionPageReqVO pageReqVO) {
        return bpmOrgDefinitionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BpmOrgDefinitionDO> getBpmOrgDefinitionList(BpmOrgDefinitionExportReqVO exportReqVO) {
        return bpmOrgDefinitionMapper.selectList(exportReqVO);
    }

}
